package com.youyi.yypermissionlibrary.core;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import com.youyi.yypermissionlibrary.core.os.OsHelper;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/* access modifiers changed from: package-private */
public class AcpManager {
    private static final int REQUEST_CODE_PERMISSION = 56;
    private static final int REQUEST_CODE_SETTING = 57;
    private static final String TAG = "AcpManager";
    private Context appContext;
    private Activity internalActivity;
    private final List<String> mDeniedPermissions = new LinkedList();
    private AcpListener mListener;
    private final Set<String> mManifestPermissions = new HashSet(1);
    private AcpOptions mOptions;

    AcpManager(Context context) {
        this.appContext = context;
        getManifestPermissions();
    }

    /* access modifiers changed from: package-private */
    public synchronized void request(AcpOptions acpOptions, AcpListener acpListener) {
        this.mListener = acpListener;
        this.mOptions = acpOptions;
        checkSelfPermission();
    }

    /* access modifiers changed from: package-private */
    public synchronized void checkRequestPermissionRationale(Activity activity) {
        boolean z;
        this.internalActivity = activity;
        Iterator<String> it = this.mDeniedPermissions.iterator();
        loop0:
        while (true) {
            z = false;
            while (true) {
                if (!it.hasNext()) {
                    break loop0;
                }
                String next = it.next();
                if (z || AcpService.shouldShowRequestPermissionRationale(this.internalActivity, next)) {
                    z = true;
                }
            }
        }
        Log.i(TAG, "rationale = " + z);
        List<String> list = this.mDeniedPermissions;
        String[] strArr = (String[]) list.toArray(new String[list.size()]);
        if (z) {
            showRationalDialog(strArr);
        } else {
            requestPermissions(strArr);
        }
    }

    /* access modifiers changed from: package-private */
    public synchronized void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        if (i == 56) {
            LinkedList linkedList = new LinkedList();
            LinkedList linkedList2 = new LinkedList();
            for (int i2 = 0; i2 < strArr.length; i2++) {
                String str = strArr[i2];
                if (iArr[i2] == 0) {
                    linkedList.add(str);
                } else {
                    linkedList2.add(str);
                }
            }
            if (!linkedList.isEmpty() && linkedList2.isEmpty()) {
                AcpListener acpListener = this.mListener;
                if (acpListener != null) {
                    acpListener.onGranted();
                }
            } else if (!linkedList2.isEmpty()) {
                showDeniedDialog(linkedList2);
            }
        }
    }

    /* access modifiers changed from: package-private */
    public synchronized void onActivityResult(int i, int i2, Intent intent) {
        if (!(this.mListener == null || this.mOptions == null)) {
            if (i == 57) {
                checkSelfPermission();
            }
        }
    }

    private synchronized void getManifestPermissions() {
        PackageInfo packageInfo;
        String[] strArr;
        try {
            packageInfo = this.appContext.getPackageManager().getPackageInfo(this.appContext.getPackageName(), 4096);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            packageInfo = null;
        }
        if (!(packageInfo == null || (strArr = packageInfo.requestedPermissions) == null)) {
            for (String str : strArr) {
                this.mManifestPermissions.add(str);
            }
        }
    }

    private synchronized void checkSelfPermission() {
        this.mDeniedPermissions.clear();
        if (Build.VERSION.SDK_INT < 23) {
            Log.i(TAG, "Build.VERSION.SDK_INT < Build.VERSION_CODES.M");
            AcpListener acpListener = this.mListener;
            if (acpListener != null) {
                acpListener.onGranted();
            }
            return;
        }
        String[] permissions = this.mOptions.getPermissions();
        for (String str : permissions) {
            if (this.mManifestPermissions.contains(str)) {
                int checkSelfPermission = AcpService.checkSelfPermission(this.appContext, str);
                Log.i(TAG, "checkSelfPermission = " + checkSelfPermission);
                if (checkSelfPermission == -1) {
                    this.mDeniedPermissions.add(str);
                }
            } else {
                throw new RuntimeException("AndroidManifest.xml no uses permission " + str);
            }
        }
        if (this.mDeniedPermissions.isEmpty()) {
            Log.i(TAG, "mDeniedPermissions.isEmpty()");
            AcpListener acpListener2 = this.mListener;
            if (acpListener2 != null) {
                acpListener2.onGranted();
            }
            return;
        }
        startAcpActivity();
    }

    private synchronized void startAcpActivity() {
        Intent intent = new Intent(this.appContext, AcpActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.appContext.startActivity(intent);
    }

    private synchronized void showRationalDialog(final String[] strArr) {
        AlertDialog create = new AlertDialog.Builder(this.internalActivity).setMessage(this.mOptions.getRationalMessage()).setPositiveButton(this.mOptions.getRationalBtnText(), new DialogInterface.OnClickListener() {
            /* class com.youyi.yypermissionlibrary.core.AcpManager.AnonymousClass1 */

            public void onClick(DialogInterface dialogInterface, int i) {
                AcpManager.this.requestPermissions(strArr);
            }
        }).create();
        create.setCancelable(this.mOptions.isDialogCancelable());
        create.setCanceledOnTouchOutside(this.mOptions.isDialogCanceledOnTouchOutside());
        create.show();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private synchronized void requestPermissions(String[] strArr) {
        AcpService.requestPermissions(this.internalActivity, strArr, 56);
    }

    private synchronized void showDeniedDialog(final List<String> list) {
        AlertDialog create = new AlertDialog.Builder(this.internalActivity).setMessage(this.mOptions.getDeniedMessage()).setNegativeButton(this.mOptions.getDeniedCloseBtn(), new DialogInterface.OnClickListener() {
            /* class com.youyi.yypermissionlibrary.core.AcpManager.AnonymousClass3 */

            public void onClick(DialogInterface dialogInterface, int i) {
                if (AcpManager.this.mListener != null) {
                    AcpManager.this.mListener.onDenied(list);
                }
            }
        }).setPositiveButton(this.mOptions.getDeniedSettingBtn(), new DialogInterface.OnClickListener() {
            /* class com.youyi.yypermissionlibrary.core.AcpManager.AnonymousClass2 */

            public void onClick(DialogInterface dialogInterface, int i) {
                AcpManager.this.startSetting();
            }
        }).create();
        create.setCancelable(this.mOptions.isDialogCancelable());
        create.setCanceledOnTouchOutside(this.mOptions.isDialogCanceledOnTouchOutside());
        create.show();
    }

    /* access modifiers changed from: package-private */
    public void onDestroy() {
        Activity activity = this.internalActivity;
        if (activity != null) {
            activity.finish();
            this.internalActivity = null;
        }
        this.mListener = null;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void startSetting() {
        OsHelper.startSetting(this.internalActivity, 57);
    }
}
